﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BaseballGame.Models;
using System.Web.Security;
using BaseballGame.Filters;
using WebMatrix.WebData;
using Microsoft.Web.WebPages.OAuth;
using DotNetOpenAuth.AspNet;
using System.Transactions;

namespace BaseballGame.Controllers
{
    [Authorize] // will restrict access to anonymous users to all methods in the controller(J.A.)
    public class ManageTeamController : Controller
    {
        private game_entitiesDbContext db = new game_entitiesDbContext();
        private UsersContext db1 = new UsersContext();
        //
        // GET: /ManageTeam/
        [InitializeSimpleMembership]
        public ActionResult Index()
        {
            var memberId = WebSecurity.GetUserId(User.Identity.Name); // grab ID of current logged in user
            UserProfile loggedInUser = db1.UserProfiles.Find(memberId);
            //loggedInUser.hasteam = false;
            //db1.SaveChanges();

            if (loggedInUser.hasteam == false)
            {
                return RedirectToAction("Create");
            }
            else
            {
                var teams = db.teams.Include(t => t.Pitcher).Include(t => t.FirstBase).Include(t => t.SecondBase).Include(t => t.ShortStop).Include(t => t.ThirdBase).Include(t => t.Catcher).Include(t => t.LeftField).Include(t => t.Rightfield).Include(t => t.CenterField).Include(t => t.B_FirstBase).Include(t => t.B_SecondBase).Include(t => t.B_ShortStop).Include(t => t.B_ThirdBase).Include(t => t.B_Catcher).Include(t => t.B_LeftField).Include(t => t.B_Midfield).Include(t => t.B_CenterField).Include(t => t.BullPen1).Include(t => t.BullPen2).Include(t => t.BullPen3).Include(t => t.BullPen4).Include(t => t.Closer).Where(t => t.teamManagerId == memberId);
                db.SaveChanges();
                return View(teams.ToList());
            }
        }

        //
        // GET: /ManageTeam/Details/5

        public ActionResult Details(int id = 0)
        {
            Team team = db.teams.Find(id);
            if (team == null)
            {
                return HttpNotFound();
            }
            return View(team);
        }

        public ActionResult PlayerDetails(int id = 0)
        {
            Player player = db.players.Find(id);
            if (player == null)
            {
                return HttpNotFound();
            }
            positions position = db.positions.Find(player.positionId);
            ViewBag.pos = position.position;
            return View(player);
        }


        public Team Assign_Team()
        {

            int memberId = WebSecurity.GetUserId(User.Identity.Name); // grab ID of current logged in user
            //var teams = db.teams.Where(t => t.teamManagerId == 0);
            Team teamcheck = db.teams.FirstOrDefault(t => t.teamManagerId == memberId); //checks if there is a team already assigned to the user
            if (teamcheck != null)
            {
                return teamcheck; // user had already a team assigned
            }
            
            //Team myteam = db.teams.First(t => t.teamManagerId == 0);
            Team myteam = db.teams.FirstOrDefault(t => t.teamManagerId == 0);

            if (myteam != null)
            {
                myteam.teamManagerId = memberId; //assigns a team with a user id
                db.SaveChanges();
                return myteam;
            }
            else
            {
                return myteam; // returns null team
            }

           // int teamid = db.teams.Where(t => t.teamManagerId == memberId).FirstOrDefault().Id;  //returns the id of the element with teamManagerId == memberId
           // Team memberteam = db.teams.Find(teamid);

           // return memberteam;
           
        }


        //
        // GET: /ManageTeam/Create

        public ActionResult Create()
        {
           
            Team team = Assign_Team();
            if (team == null)
            {
                return RedirectToAction("index","Home");  //no teams left
            }

            ViewBag.message = "Please Assign players a position";

            ViewBag.PitcherId = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.PitcherId);
            ViewBag.FirstBaseId = new SelectList(db.players.Where(t => t.positionId == 2 && t.teamId == team.Id), "Id", "player_name", team.FirstBaseId);
            ViewBag.SecondBaseId = new SelectList(db.players.Where(t => t.positionId == 3 && t.teamId == team.Id), "Id", "player_name", team.SecondBaseId);
            ViewBag.ShortStopId = new SelectList(db.players.Where(t => t.positionId == 5 && t.teamId == team.Id), "Id", "player_name", team.ShortStopId);
            ViewBag.ThirdBaseId = new SelectList(db.players.Where(t => t.positionId == 4 && t.teamId == team.Id), "Id", "player_name", team.ThirdBaseId);
            ViewBag.CatcherId = new SelectList(db.players.Where(t => t.positionId == 6 && t.teamId == team.Id), "Id", "player_name", team.CatcherId);
            ViewBag.LeftFieldId = new SelectList(db.players.Where(t => t.positionId == 7 && t.teamId == team.Id), "Id", "player_name", team.LeftFieldId);
            ViewBag.RightfieldId = new SelectList(db.players.Where(t => t.positionId == 9 && t.teamId == team.Id), "Id", "player_name", team.RightfieldId);
            ViewBag.CenterFieldId = new SelectList(db.players.Where(t => t.positionId == 8 && t.teamId == team.Id), "Id", "player_name", team.CenterFieldId);
            ViewBag.B_FirstBaseId = new SelectList(db.players.Where(t => t.positionId == 2 && t.teamId == team.Id), "Id", "player_name", team.B_FirstBaseId);
            ViewBag.B_SecondBaseId = new SelectList(db.players.Where(t => t.positionId == 3 && t.teamId == team.Id), "Id", "player_name", team.B_SecondBaseId);
            ViewBag.B_ShortStopId = new SelectList(db.players.Where(t => t.positionId == 5 && t.teamId == team.Id), "Id", "player_name", team.B_ShortStopId);
            ViewBag.B_ThirdBaseId = new SelectList(db.players.Where(t => t.positionId == 4 && t.teamId == team.Id), "Id", "player_name", team.B_ThirdBaseId);
            ViewBag.B_CatcherId = new SelectList(db.players.Where(t => t.positionId == 6 && t.teamId == team.Id), "Id", "player_name", team.B_CatcherId);
            ViewBag.B_LeftFieldId = new SelectList(db.players.Where(t => t.positionId == 7 && t.teamId == team.Id), "Id", "player_name", team.B_LeftFieldId);
            ViewBag.B_MidfieldId = new SelectList(db.players.Where(t => t.positionId == 9 && t.teamId == team.Id), "Id", "player_name", team.B_MidfieldId);
            ViewBag.B_CenterFieldId = new SelectList(db.players.Where(t => t.positionId == 8 && t.teamId == team.Id), "Id", "player_name", team.B_CenterFieldId);
            ViewBag.BullPen1Id = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.BullPen1Id);
            ViewBag.BullPen2Id = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.BullPen2Id);
            ViewBag.BullPen3Id = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.BullPen3Id);
            ViewBag.BullPen4Id = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.BullPen4Id);
            ViewBag.CloserId = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.CloserId);

            return View(team);
        }

        //
        // POST: /ManageTeam/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        [InitializeSimpleMembership]
        public ActionResult Create(Team team)
        {
            var memberId = WebSecurity.GetUserId(User.Identity.Name); // grab ID of current logged in user
            UserProfile loggedInUser = db1.UserProfiles.Find(memberId);

            if (loggedInUser.hasteam == true)
            {

                ViewBag.hasteam = "Only One Team per User Is Allowed";
                return RedirectToAction("Index","Home");
            }
            else
            {

                if (ModelState.IsValid)
                {
                    int[] numbers = new int[22];  //array declaration
                    numbers[0] = team.PitcherId;
                    numbers[1] = team.FirstBaseId;
                    numbers[2] = team.SecondBaseId;
                    numbers[3] = team.ShortStopId;
                    numbers[4] = team.ThirdBaseId;
                    numbers[5] = team.LeftFieldId;
                    numbers[6] = team.RightfieldId;
                    numbers[7] = team.CenterFieldId;
                    numbers[8] = team.CatcherId;
                    numbers[9] = team.B_CatcherId;
                    numbers[10] = team.B_FirstBaseId;
                    numbers[11] = team.B_SecondBaseId;
                    numbers[12] = team.B_ThirdBaseId;
                    numbers[13] = team.B_ShortStopId;
                    numbers[14] = team.B_MidfieldId;
                    numbers[15] = team.B_LeftFieldId;
                    numbers[16] = team.B_CenterFieldId;
                    numbers[17] = team.BullPen1Id;
                    numbers[18] = team.BullPen2Id;
                    numbers[19] = team.BullPen3Id;
                    numbers[20] = team.BullPen4Id;
                    numbers[21] = team.CloserId;

                   
                    if (numbers.Distinct().Count() == numbers.Count())
                    {
                        //does not contain duplicates

                       
                        db.Entry(team).State = EntityState.Modified;
                        db.SaveChanges();
                        loggedInUser.hasteam = true;
                        db1.SaveChanges();
                        return RedirectToAction("Index");
                        
                    }
                    else
                    {
                        ViewBag.message = "The same player cannot player multiple positions";
                       
                        ViewBag.PitcherId = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.PitcherId);
                        ViewBag.FirstBaseId = new SelectList(db.players.Where(t => t.positionId == 2 && t.teamId == team.Id), "Id", "player_name", team.FirstBaseId);
                        ViewBag.SecondBaseId = new SelectList(db.players.Where(t => t.positionId == 3 && t.teamId == team.Id), "Id", "player_name", team.SecondBaseId);
                        ViewBag.ShortStopId = new SelectList(db.players.Where(t => t.positionId == 5 && t.teamId == team.Id), "Id", "player_name", team.ShortStopId);
                        ViewBag.ThirdBaseId = new SelectList(db.players.Where(t => t.positionId == 4 && t.teamId == team.Id), "Id", "player_name", team.ThirdBaseId);
                        ViewBag.CatcherId = new SelectList(db.players.Where(t => t.positionId == 6 && t.teamId == team.Id), "Id", "player_name", team.CatcherId);
                        ViewBag.LeftFieldId = new SelectList(db.players.Where(t => t.positionId == 7 && t.teamId == team.Id), "Id", "player_name", team.LeftFieldId);
                        ViewBag.RightfieldId = new SelectList(db.players.Where(t => t.positionId == 9 && t.teamId == team.Id), "Id", "player_name", team.RightfieldId);
                        ViewBag.CenterFieldId = new SelectList(db.players.Where(t => t.positionId == 8 && t.teamId == team.Id), "Id", "player_name", team.CenterFieldId);
                        ViewBag.B_FirstBaseId = new SelectList(db.players.Where(t => t.positionId == 2 && t.teamId == team.Id), "Id", "player_name", team.B_FirstBaseId);
                        ViewBag.B_SecondBaseId = new SelectList(db.players.Where(t => t.positionId == 3 && t.teamId == team.Id), "Id", "player_name", team.B_SecondBaseId);
                        ViewBag.B_ShortStopId = new SelectList(db.players.Where(t => t.positionId == 5 && t.teamId == team.Id), "Id", "player_name", team.B_ShortStopId);
                        ViewBag.B_ThirdBaseId = new SelectList(db.players.Where(t => t.positionId == 4 && t.teamId == team.Id), "Id", "player_name", team.B_ThirdBaseId);
                        ViewBag.B_CatcherId = new SelectList(db.players.Where(t => t.positionId == 6 && t.teamId == team.Id), "Id", "player_name", team.B_CatcherId);
                        ViewBag.B_LeftFieldId = new SelectList(db.players.Where(t => t.positionId == 7 && t.teamId == team.Id), "Id", "player_name", team.B_LeftFieldId);
                        ViewBag.B_MidfieldId = new SelectList(db.players.Where(t => t.positionId == 9 && t.teamId == team.Id), "Id", "player_name", team.B_MidfieldId);
                        ViewBag.B_CenterFieldId = new SelectList(db.players.Where(t => t.positionId == 8 && t.teamId == team.Id), "Id", "player_name", team.B_CenterFieldId);
                        ViewBag.BullPen1Id = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.BullPen1Id);
                        ViewBag.BullPen2Id = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.BullPen2Id);
                        ViewBag.BullPen3Id = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.BullPen3Id);
                        ViewBag.BullPen4Id = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.BullPen4Id);
                        ViewBag.CloserId = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.CloserId);
                        return View(team);

                    }
               
                }
                else
                {
                    
                    ViewBag.PitcherId = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.PitcherId);
                    ViewBag.FirstBaseId = new SelectList(db.players.Where(t => t.positionId == 2 && t.teamId == team.Id), "Id", "player_name", team.FirstBaseId);
                    ViewBag.SecondBaseId = new SelectList(db.players.Where(t => t.positionId == 3 && t.teamId == team.Id), "Id", "player_name", team.SecondBaseId);
                    ViewBag.ShortStopId = new SelectList(db.players.Where(t => t.positionId == 5 && t.teamId == team.Id), "Id", "player_name", team.ShortStopId);
                    ViewBag.ThirdBaseId = new SelectList(db.players.Where(t => t.positionId == 4 && t.teamId == team.Id), "Id", "player_name", team.ThirdBaseId);
                    ViewBag.CatcherId = new SelectList(db.players.Where(t => t.positionId == 6 && t.teamId == team.Id), "Id", "player_name", team.CatcherId);
                    ViewBag.LeftFieldId = new SelectList(db.players.Where(t => t.positionId == 7 && t.teamId == team.Id), "Id", "player_name", team.LeftFieldId);
                    ViewBag.RightfieldId = new SelectList(db.players.Where(t => t.positionId == 9 && t.teamId == team.Id), "Id", "player_name", team.RightfieldId);
                    ViewBag.CenterFieldId = new SelectList(db.players.Where(t => t.positionId == 8 && t.teamId == team.Id), "Id", "player_name", team.CenterFieldId);
                    ViewBag.B_FirstBaseId = new SelectList(db.players.Where(t => t.positionId == 2 && t.teamId == team.Id), "Id", "player_name", team.B_FirstBaseId);
                    ViewBag.B_SecondBaseId = new SelectList(db.players.Where(t => t.positionId == 3 && t.teamId == team.Id), "Id", "player_name", team.B_SecondBaseId);
                    ViewBag.B_ShortStopId = new SelectList(db.players.Where(t => t.positionId == 5 && t.teamId == team.Id), "Id", "player_name", team.B_ShortStopId);
                    ViewBag.B_ThirdBaseId = new SelectList(db.players.Where(t => t.positionId == 4 && t.teamId == team.Id), "Id", "player_name", team.B_ThirdBaseId);
                    ViewBag.B_CatcherId = new SelectList(db.players.Where(t => t.positionId == 6 && t.teamId == team.Id), "Id", "player_name", team.B_CatcherId);
                    ViewBag.B_LeftFieldId = new SelectList(db.players.Where(t => t.positionId == 7 && t.teamId == team.Id), "Id", "player_name", team.B_LeftFieldId);
                    ViewBag.B_MidfieldId = new SelectList(db.players.Where(t => t.positionId == 9 && t.teamId == team.Id), "Id", "player_name", team.B_MidfieldId);
                    ViewBag.B_CenterFieldId = new SelectList(db.players.Where(t => t.positionId == 8 && t.teamId == team.Id), "Id", "player_name", team.B_CenterFieldId);
                    ViewBag.BullPen1Id = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.BullPen1Id);
                    ViewBag.BullPen2Id = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.BullPen2Id);
                    ViewBag.BullPen3Id = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.BullPen3Id);
                    ViewBag.BullPen4Id = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.BullPen4Id);
                    ViewBag.CloserId = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.CloserId);
                    return View(team);
                }

            }

        }

        //
        // GET: /ManageTeam/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Team team = db.teams.Find(id);
            if (team == null)
            {
                return HttpNotFound();
            }
            ViewBag.PitcherId = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.PitcherId);
            ViewBag.FirstBaseId = new SelectList(db.players.Where(t => t.positionId == 2 && t.teamId == team.Id), "Id", "player_name", team.FirstBaseId);
            ViewBag.SecondBaseId = new SelectList(db.players.Where(t => t.positionId == 3 && t.teamId == team.Id), "Id", "player_name", team.SecondBaseId);
            ViewBag.ShortStopId = new SelectList(db.players.Where(t => t.positionId == 5 && t.teamId == team.Id), "Id", "player_name", team.ShortStopId);
            ViewBag.ThirdBaseId = new SelectList(db.players.Where(t => t.positionId == 4 && t.teamId == team.Id), "Id", "player_name", team.ThirdBaseId);
            ViewBag.CatcherId = new SelectList(db.players.Where(t => t.positionId == 6 && t.teamId == team.Id), "Id", "player_name", team.CatcherId);
            ViewBag.LeftFieldId = new SelectList(db.players.Where(t => t.positionId == 7 && t.teamId == team.Id), "Id", "player_name", team.LeftFieldId);
            ViewBag.RightfieldId = new SelectList(db.players.Where(t => t.positionId == 9 && t.teamId == team.Id), "Id", "player_name", team.RightfieldId);
            ViewBag.CenterFieldId = new SelectList(db.players.Where(t => t.positionId == 8 && t.teamId == team.Id), "Id", "player_name", team.CenterFieldId);
            ViewBag.B_FirstBaseId = new SelectList(db.players.Where(t => t.positionId == 2 && t.teamId == team.Id), "Id", "player_name", team.B_FirstBaseId);
            ViewBag.B_SecondBaseId = new SelectList(db.players.Where(t => t.positionId == 3 && t.teamId == team.Id), "Id", "player_name", team.B_SecondBaseId);
            ViewBag.B_ShortStopId = new SelectList(db.players.Where(t => t.positionId == 5 && t.teamId == team.Id), "Id", "player_name", team.B_ShortStopId);
            ViewBag.B_ThirdBaseId = new SelectList(db.players.Where(t => t.positionId == 4 && t.teamId == team.Id), "Id", "player_name", team.B_ThirdBaseId);
            ViewBag.B_CatcherId = new SelectList(db.players.Where(t => t.positionId == 6 && t.teamId == team.Id), "Id", "player_name", team.B_CatcherId);
            ViewBag.B_LeftFieldId = new SelectList(db.players.Where(t => t.positionId == 7 && t.teamId == team.Id), "Id", "player_name", team.B_LeftFieldId);
            ViewBag.B_MidfieldId = new SelectList(db.players.Where(t => t.positionId == 9 && t.teamId == team.Id), "Id", "player_name", team.B_MidfieldId);
            ViewBag.B_CenterFieldId = new SelectList(db.players.Where(t => t.positionId == 8 && t.teamId == team.Id), "Id", "player_name", team.B_CenterFieldId);
            ViewBag.BullPen1Id = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.BullPen1Id);
            ViewBag.BullPen2Id = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.BullPen2Id);
            ViewBag.BullPen3Id = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.BullPen3Id);
            ViewBag.BullPen4Id = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.BullPen4Id);
            ViewBag.CloserId = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.CloserId);
            return View(team);
        }

        //
        // POST: /ManageTeam/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [InitializeSimpleMembership] //added 
        public ActionResult Edit(Team team)
        {
            if (ModelState.IsValid)
            {
             
                int[] numbers = new int[22];  //array declaration
                numbers[0] = team.PitcherId;
                numbers[1] = team.FirstBaseId;
                numbers[2] = team.SecondBaseId;
                numbers[3] = team.ShortStopId;
                numbers[4] = team.ThirdBaseId;
                numbers[5] = team.LeftFieldId;
                numbers[6] = team.RightfieldId;
                numbers[7] = team.CenterFieldId;
                numbers[8] = team.CatcherId;
                numbers[9] = team.B_CatcherId;
                numbers[10] = team.B_FirstBaseId;
                numbers[11] = team.B_SecondBaseId;
                numbers[12] = team.B_ThirdBaseId;
                numbers[13] = team.B_ShortStopId;
                numbers[14] = team.B_MidfieldId;
                numbers[15] = team.B_LeftFieldId;
                numbers[16] = team.B_CenterFieldId;
                numbers[17] = team.BullPen1Id;
                numbers[18] = team.BullPen2Id;
                numbers[19] = team.BullPen3Id;
                numbers[20] = team.BullPen4Id;
                numbers[21] = team.CloserId;


                if (numbers.Distinct().Count() == numbers.Count())
                {
                    //does not contain duplicates

                    var memberId = WebSecurity.GetUserId(User.Identity.Name); // grab ID of current logged in user
                    team.teamManagerId = memberId; // must be done otherwise it'll set teamManagerId to zero
                    db.Entry(team).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                   

                }
                else
                {
                    ViewBag.message = "The same player cannot player multiple positions";

                    ViewBag.PitcherId = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.PitcherId);
                    ViewBag.FirstBaseId = new SelectList(db.players.Where(t => t.positionId == 2 && t.teamId == team.Id), "Id", "player_name", team.FirstBaseId);
                    ViewBag.SecondBaseId = new SelectList(db.players.Where(t => t.positionId == 3 && t.teamId == team.Id), "Id", "player_name", team.SecondBaseId);
                    ViewBag.ShortStopId = new SelectList(db.players.Where(t => t.positionId == 5 && t.teamId == team.Id), "Id", "player_name", team.ShortStopId);
                    ViewBag.ThirdBaseId = new SelectList(db.players.Where(t => t.positionId == 4 && t.teamId == team.Id), "Id", "player_name", team.ThirdBaseId);
                    ViewBag.CatcherId = new SelectList(db.players.Where(t => t.positionId == 6 && t.teamId == team.Id), "Id", "player_name", team.CatcherId);
                    ViewBag.LeftFieldId = new SelectList(db.players.Where(t => t.positionId == 7 && t.teamId == team.Id), "Id", "player_name", team.LeftFieldId);
                    ViewBag.RightfieldId = new SelectList(db.players.Where(t => t.positionId == 9 && t.teamId == team.Id), "Id", "player_name", team.RightfieldId);
                    ViewBag.CenterFieldId = new SelectList(db.players.Where(t => t.positionId == 8 && t.teamId == team.Id), "Id", "player_name", team.CenterFieldId);
                    ViewBag.B_FirstBaseId = new SelectList(db.players.Where(t => t.positionId == 2 && t.teamId == team.Id), "Id", "player_name", team.B_FirstBaseId);
                    ViewBag.B_SecondBaseId = new SelectList(db.players.Where(t => t.positionId == 3 && t.teamId == team.Id), "Id", "player_name", team.B_SecondBaseId);
                    ViewBag.B_ShortStopId = new SelectList(db.players.Where(t => t.positionId == 5 && t.teamId == team.Id), "Id", "player_name", team.B_ShortStopId);
                    ViewBag.B_ThirdBaseId = new SelectList(db.players.Where(t => t.positionId == 4 && t.teamId == team.Id), "Id", "player_name", team.B_ThirdBaseId);
                    ViewBag.B_CatcherId = new SelectList(db.players.Where(t => t.positionId == 6 && t.teamId == team.Id), "Id", "player_name", team.B_CatcherId);
                    ViewBag.B_LeftFieldId = new SelectList(db.players.Where(t => t.positionId == 7 && t.teamId == team.Id), "Id", "player_name", team.B_LeftFieldId);
                    ViewBag.B_MidfieldId = new SelectList(db.players.Where(t => t.positionId == 9 && t.teamId == team.Id), "Id", "player_name", team.B_MidfieldId);
                    ViewBag.B_CenterFieldId = new SelectList(db.players.Where(t => t.positionId == 8 && t.teamId == team.Id), "Id", "player_name", team.B_CenterFieldId);
                    ViewBag.BullPen1Id = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.BullPen1Id);
                    ViewBag.BullPen2Id = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.BullPen2Id);
                    ViewBag.BullPen3Id = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.BullPen3Id);
                    ViewBag.BullPen4Id = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.BullPen4Id);
                    ViewBag.CloserId = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.CloserId);
                    return View(team);

                }


            }

            ViewBag.PitcherId = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.PitcherId);
            ViewBag.FirstBaseId = new SelectList(db.players.Where(t => t.positionId == 2 && t.teamId == team.Id), "Id", "player_name", team.FirstBaseId);
            ViewBag.SecondBaseId = new SelectList(db.players.Where(t => t.positionId == 3 && t.teamId == team.Id), "Id", "player_name", team.SecondBaseId);
            ViewBag.ShortStopId = new SelectList(db.players.Where(t => t.positionId == 5 && t.teamId == team.Id), "Id", "player_name", team.ShortStopId);
            ViewBag.ThirdBaseId = new SelectList(db.players.Where(t => t.positionId == 4 && t.teamId == team.Id), "Id", "player_name", team.ThirdBaseId);
            ViewBag.CatcherId = new SelectList(db.players.Where(t => t.positionId == 6 && t.teamId == team.Id), "Id", "player_name", team.CatcherId);
            ViewBag.LeftFieldId = new SelectList(db.players.Where(t => t.positionId == 7 && t.teamId == team.Id), "Id", "player_name", team.LeftFieldId);
            ViewBag.RightfieldId = new SelectList(db.players.Where(t => t.positionId == 9 && t.teamId == team.Id), "Id", "player_name", team.RightfieldId);
            ViewBag.CenterFieldId = new SelectList(db.players.Where(t => t.positionId == 8 && t.teamId == team.Id), "Id", "player_name", team.CenterFieldId);
            ViewBag.B_FirstBaseId = new SelectList(db.players.Where(t => t.positionId == 2 && t.teamId == team.Id), "Id", "player_name", team.B_FirstBaseId);
            ViewBag.B_SecondBaseId = new SelectList(db.players.Where(t => t.positionId == 3 && t.teamId == team.Id), "Id", "player_name", team.B_SecondBaseId);
            ViewBag.B_ShortStopId = new SelectList(db.players.Where(t => t.positionId == 5 && t.teamId == team.Id), "Id", "player_name", team.B_ShortStopId);
            ViewBag.B_ThirdBaseId = new SelectList(db.players.Where(t => t.positionId == 4 && t.teamId == team.Id), "Id", "player_name", team.B_ThirdBaseId);
            ViewBag.B_CatcherId = new SelectList(db.players.Where(t => t.positionId == 6 && t.teamId == team.Id), "Id", "player_name", team.B_CatcherId);
            ViewBag.B_LeftFieldId = new SelectList(db.players.Where(t => t.positionId == 7 && t.teamId == team.Id), "Id", "player_name", team.B_LeftFieldId);
            ViewBag.B_MidfieldId = new SelectList(db.players.Where(t => t.positionId == 9 && t.teamId == team.Id), "Id", "player_name", team.B_MidfieldId);
            ViewBag.B_CenterFieldId = new SelectList(db.players.Where(t => t.positionId == 8 && t.teamId == team.Id), "Id", "player_name", team.B_CenterFieldId);
            ViewBag.BullPen1Id = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.BullPen1Id);
            ViewBag.BullPen2Id = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.BullPen2Id);
            ViewBag.BullPen3Id = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.BullPen3Id);
            ViewBag.BullPen4Id = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.BullPen4Id);
            ViewBag.CloserId = new SelectList(db.players.Where(t => t.positionId == 1 && t.teamId == team.Id), "Id", "player_name", team.CloserId);
            return View(team);
        }

        //
        // GET: /ManageTeam/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Team team = db.teams.Find(id);
            if (team == null)
            {
                return HttpNotFound();
            }
            return View(team);
        }

        //
        // POST: /ManageTeam/Delete/5
        [InitializeSimpleMembership]
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            var memberId = WebSecurity.GetUserId(User.Identity.Name); // grab ID of current logged in user
            UserProfile loggedInUser = db1.UserProfiles.Find(memberId); //creates an instance of the current logged in user
            loggedInUser.hasteam = false; // flag that user does not have a team anymore
            db1.SaveChanges();

            Team team = db.teams.Find(id);
            db.teams.Remove(team);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}